/**
 * 
 */
package sob.netcode.threadapp;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author blackcore
 * @date 2013-11-15
 * @describe
 */
public class ImplExecutor {
	public void SingleThread() {
		ImplRunnable runnable = new ImplRunnable();

		ExecutorService executor = Executors.newSingleThreadExecutor();
		executor.execute(runnable);
		executor.shutdown();
	}

	public void MutiThread() {
		List<ImplRunnable> runnables = new ArrayList<ImplRunnable>();
		runnables.add(new ImplRunnable());
		runnables.add(new ImplRunnable());

		ExecutorService executor = Executors.newFixedThreadPool(5);
		for (Runnable runnable : runnables) {
			executor.execute(runnable);
		}
		// System.out.println("isTerminated:\t" + executor.isTerminated());
		executor.shutdown();
	}

	/**
	 * 定时调试
	 */
	public void ScheduleThread() {
		ImplRunnable runnable = new ImplRunnable();

		ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
		executor.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.MINUTES);
		executor.shutdown();
	}

	/**
	 * 不调用ExecutorService.shutdown()或者ExecutorService.shutdownNow()方法，主线程将会一直阻塞住
	 * 
	 * @param threadCount
	 */
	public void CloseExecutorService(Integer threadCount) {
		ExecutorService executor = Executors.newFixedThreadPool(threadCount);
		for (int i = 0; i < threadCount; i++) {
			ImplRunnable runnable = new ImplRunnable();
			executor.execute(runnable);
		}
		executor.shutdown();
		while (!executor.isTerminated()) {
			System.out.println("Finished all threads");
		}
	}
}
